﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace FoodManager
{
    public enum TrangThai
    {
        Hide=0,
        Show=1,
        Hot=2
    }
    public partial class frNhapSanPham : Form
    {
        public frNhapSanPham()
        {
            InitializeComponent();
            dbTableAdapters.tblloaisanphamTableAdapter da=new dbTableAdapters.tblloaisanphamTableAdapter();
            db.tblloaisanphamDataTable loaidata = da.GetData();
            AutoCompleteStringCollection acsc = new AutoCompleteStringCollection();
            foreach (DataRow r in loaidata.Rows)
            {
                 acsc.Add(r["loai"].ToString());
            }
            txtLoai.AutoCompleteCustomSource = acsc;
            txtLoai.AutoCompleteMode = AutoCompleteMode.SuggestAppend;
            txtLoai.AutoCompleteSource = AutoCompleteSource.CustomSource;
        }
        void BindData()
        {
            //tblsanphamTableAdapter.Fill(this.db.tblsanpham);
            //tblsanphamBindingSource.DataSource = db;
            //tblsanphamBindingSource.DataMember = "tblsanpham";
            //listBox1.DataSource = tblsanphamBindingSource;
            //listBox1.DisplayMember = "ten";
            //listBox1.ValueMember = "id";
            //tblsanphamBindingSource.ResetBindings(false);
            
        }
        private void frNhapSanPhamChoDonHang_Load(object sender, EventArgs e)
        {
            // TODO: This line of code loads data into the 'db.tblsanpham' table. You can move, or remove it, as needed.
            this.tblsanphamTableAdapter.Fill(this.db.tblsanpham);
            BindData();
            cbTrangThai.Items.Add("Hiện");
            cbTrangThai.Items.Add("Ẩn");
            cbTrangThai.Items.Add("Nổi bật");
        }

        private void btnInsert_Click(object sender, EventArgs e)
        {
           tblsanphamTableAdapter.Insert(txtMaSanPham.Text, txtTenSanPham.Text, txtLoai.Text, cbDonVi.Text, Convert.ToDecimal(txtGia.Text), Convert.ToByte(Functions.GetIntTrangThai(cbTrangThai.Text)));
           //listBox1.Items.Clear();
           BindData();
        }

        private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            DataTable data = tblsanphamTableAdapter.GetDataById(Convert.ToInt32(listBox1.SelectedValue));
            if (data.Rows.Count > 0)
            {
                txtGia.Text = Convert.ToInt32(data.Rows[0]["dongia"]).ToString();
                txtLoai.Text = data.Rows[0]["loai"].ToString();
                txtMaSanPham.Text = data.Rows[0]["masanpham"].ToString();
                txtTenSanPham.Text = data.Rows[0]["ten"].ToString();
                cbDonVi.Text = data.Rows[0]["donvi"].ToString();
                cbTrangThai.Text = Functions.GetStringTrangThai(Convert.ToInt32(data.Rows[0]["trangthai"]));
            }
        }

        private void btnUpdate_Click(object sender, EventArgs e)
        {
            tblsanphamTableAdapter.UpdateData(txtMaSanPham.Text, txtTenSanPham.Text, txtLoai.Text, cbDonVi.Text, Convert.ToDecimal(txtGia.Text), Convert.ToByte(Functions.GetIntTrangThai(cbTrangThai.Text)), Convert.ToInt32(listBox1.SelectedValue));
            //listBox1.Items.Clear();
            BindData();
            MessageBox.Show("Cập nhật thành công");
           
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("Bạn có chắc muốn xóa sản phẩm này không?", "Message", MessageBoxButtons.YesNo) == DialogResult.Yes)
            {
                try
                {
                    tblsanphamTableAdapter.DeleteQuery(Convert.ToInt32(listBox1.SelectedValue));
                   // listBox1.Items.Clear();
                    BindData();
                }
                catch
                {
                    MessageBox.Show("Sản phẩm này đã được đặt hàng nên không thể xóa khỏi hệ thống.");
                }
            }
        }
    }
}
